package myqueue;

public class MyCircularQueue {
    public int[] elem;
    public int front;
    public int rear;

    public MyCircularQueue(int k) {
        elem = new int[k + 1];
    }

    /**
     * 入队操作
     * @param value
     * @return
     */
    public boolean enQueue(int value) {
        if(isFull()) {
            return false;
        }

        elem[rear] = value;
        rear = (rear + 1) % elem.length;
        return true;
    }

    /**
     * 出队操作
     * @return
     */
    public boolean deQueue() {
        if(isEmpty()) {
            throw new EmptyException("队列为空");
        }

        int val = elem[front];
        front = (front + 1) % elem.length;
        return true;
    }

    /**
     * 返回front位置元素,不删除
     * @return
     */
    public int Front() {
        if(isEmpty()) {
            throw new EmptyException("队列为空");
        }

        return elem[front];
    }

    /**
     * 返回rear位置元素,不删除
     * @return
     */
    public int Rear() {
        if(isEmpty()) {
            throw new EmptyException("队列为空");
        }

        int index = rear == 0 ? elem.length - 1 : rear - 1;
        return elem[index];
    }

    /**
     * 判空
     * @return
     */
    public boolean isEmpty() {
        return rear == front;
    }

    /**
     * 判满
     * @return
     */
    public boolean isFull() {
        return (rear + 1) % elem.length == front;
    }
}